package com.scqkxx.gamebox.activity.myself;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.activity.game.adapter.GameAdapter;
import com.scqkxx.gamebox.activity.myself.adapter.SpaceAdapter;
import com.scqkxx.gamebox.activity.myself.address.SpaceBean;
import com.scqkxx.gamebox.activity.space.SearchActivity;
import com.scqkxx.gamebox.activity.webview.web.WebViewActivity;
import com.scqkxx.gamebox.activity.webview.web.model.WebViewConfig;
import com.scqkxx.gamebox.bean.EvenBusBean;
import com.scqkxx.gamebox.databinding.ActivityMyselfSettingBinding;
import com.scqkxx.gamebox.utils.DialogUtils;
import com.scqkzqtz.base.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *空间列表
 */
public class MySelfSettingActivity extends BaseActivity<ActivityMyselfSettingBinding> implements View.OnClickListener {
    private List<SpaceBean> mGameListData;
    public SpaceAdapter spaceAdapter;


    @Override
    public int setLayoutId() { return R.layout.activity_myself_setting; }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        initData();
       initEner();
    }

    public void initEner() {
        binding.serch.setOnClickListener(v -> {
            //跳转搜索
            ActivityUtil.switchTo(MySelfSettingActivity.this, SearchActivity.class);
        });
        binding.tvTitleView.getTv_right().setVisibility(View.GONE);
        binding.tvTitleView.setOnTitleClickListener(mTitleType -> {
            if (mTitleType == TitleView.CLICK_BACK){
                finish();
            }
        });
    }

    private void initData() {
        mGameListData = new ArrayList<>();
        spaceAdapter = new SpaceAdapter( R.layout.item_space , mGameListData);
        GridLayoutManager linearLayoutManager = new GridLayoutManager(this,2);
        binding.mRecyclerView.setLayoutManager(linearLayoutManager);
        binding.mRecyclerView.setAdapter(spaceAdapter);
        loadData();
    }

    private void loadData() {
        mGameListData.clear();
        DialogUtils.showDialog(this);
        AVQuery<AVObject> query = AVQuery.getQuery("Space");
        query.whereEqualTo("isDisable" , 0);
        query.findInBackground( new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e ) {
                if (e  == null){
                    for (int i = 0; i < list.size() ; i++) {
                        AVObject avObject = list.get(i);
                        SpaceBean spaceBean =  new SpaceBean();
                        spaceBean.setType(0);
                        spaceBean.setPositionNum((Integer) avObject.get("positionNum"));
                        spaceBean.setMembersNum((Integer) avObject.get("membersNum"));
                        spaceBean.setGoodsNum((Integer) avObject.get("goodsNum"));
                        spaceBean.setName((String) avObject.get("name"));
                        spaceBean.setImageUrl((String) avObject.get("avatar"));
                        spaceBean.setBelongUserName((String) avObject.get("belongUserName"));
                        spaceBean.setBelongGroupId((Integer) avObject.get("belongGroupId"));
                        spaceBean.setObjectId(avObject.getObjectId());
                        spaceBean.setSpaceId(Integer.valueOf(avObject.getObjectId()));
                        spaceBean.setCreateTime(new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(list.get(i).getCreatedAt()));
                        mGameListData.add(spaceBean);
                    }
                }
                SpaceBean spaceBean =  new SpaceBean();
                spaceBean.setType(1);
                spaceBean.setBelongGroupId(AVUser.getCurrentUser().getInt("defaultGroupId"));
                mGameListData.add(spaceBean);
                spaceAdapter.setNewData(mGameListData);
                DialogUtils.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {

    }


    @Subscribe
    public void changeListData(EvenBusBean evenBusBean){
        if (evenBusBean.getState() == 201) {
            loadData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
